package com.github.llyb120.sxmanage;

import cn.hutool.core.util.IdUtil;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;

/**
 * @Author: Administrator
 * @Date: 2020/12/12 16:01
 */
@CrossOrigin
@RequestMapping("/")
@RestController
public class FileController {

    @Autowired
    LoginController loginController;

    static String path = System.getProperty("user.dir");
    static {
        File file = new File(path, "patch");
        file.mkdirs();
        path = file.getAbsolutePath();
    }

    @RequestMapping("upload")
    public Object upload(
        MultipartFile file,
        String token
    ) throws IOException {
        if(!loginController.checkLogin(token)){
            throw new RuntimeException();
        }
        String fid = IdUtil.objectId();
        File target = new File(path, fid);
        file.transferTo(target);
        return new HashMap(){
            {
                put("fid", fid);
            }
        };
    }
}
